using System;
using System.Xml;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;

namespace SBPweb.Controls.Windows.Docking
{
	/// <summary>
	/// Class of SkinnedRenderer
	/// </summary>
	public class SkinnedRenderer : TD.SandDock.Rendering.WhidbeyRenderer, SBPweb.Themes.Windows.Skins.ISkinnable
	{
		private bool mUseDefaultSkin;
		private Skin mSkin;
		private bool mUseDefaultSkinsEventInUse = false;

		#region Cache

		private static Image classCacheDockWindowCaptionImgage;
		private Image objectCacheDockWindowCaptionImgage;
		private Image CacheDockWindowCaptionImgage
		{
			get
			{
				if (UseDefaultSkin) return classCacheDockWindowCaptionImgage;
				else return objectCacheDockWindowCaptionImgage;
			}
		}

		private static bool classCacheDockWindowCaptionVerticalLayout;
		private bool objectCacheDockWindowCaptionVerticalLayout;
		private bool CacheDockWindowCaptionVerticalLayout
		{
			get
			{
				if (UseDefaultSkin) return classCacheDockWindowCaptionVerticalLayout;
				else return objectCacheDockWindowCaptionVerticalLayout;
			}
		}

		private static Margin classCacheDockWindowCaptionSizingMargin;
		private Margin objectCacheDockWindowCaptionSizingMargin;
		private Margin CacheDockWindowCaptionSizingMargin
		{
			get
			{
				if (UseDefaultSkin) return classCacheDockWindowCaptionSizingMargin;
				else return objectCacheDockWindowCaptionSizingMargin;
			}
		}

		
		private static Image classCacheTabHeaderImgage;
		private Image objectCacheTabHeaderImgage;
		private Image CacheTabHeaderImgage
		{
			get
			{
				if (UseDefaultSkin) return classCacheTabHeaderImgage;
				else return objectCacheTabHeaderImgage;
			}
		}
		
		private static bool classCacheTabHeaderVerticalLayout;
		private bool objectCacheTabHeaderVerticalLayout;
		private bool CacheTabHeaderVerticalLayout
		{
			get
			{
				if (UseDefaultSkin) return classCacheTabHeaderVerticalLayout;
				else return objectCacheTabHeaderVerticalLayout;
			}
		}

		private static Margin classCacheTabHeaderSizingMargin;
		private Margin objectCacheTabHeaderSizingMargin;
		private Margin CacheTabHeaderSizingMargin
		{
			get 
			{
				if (UseDefaultSkin) return classCacheTabHeaderSizingMargin;
				else return objectCacheTabHeaderSizingMargin;
			}
		}

		private static Image classCacheTabStripTabImgage;
		private Image objectCacheTabStripTabImgage;
		private Image CacheTabStripTabImgage
		{
			get
			{
				if (UseDefaultSkin) return classCacheTabStripTabImgage;
				else return objectCacheTabStripTabImgage;
			}
		}

		private static bool classCacheTabStripTabVerticalLayout;
		private bool objectCacheTabStripTabVerticalLayout;
		private bool CacheTabStripTabVerticalLayout
		{
			get
			{
				if (UseDefaultSkin) return classCacheTabStripTabVerticalLayout;
				else return objectCacheTabStripTabVerticalLayout;
			}
		}

		private static Margin classCacheTabStripTabSizingMargin;
		private Margin objectCacheTabStripTabSizingMargin;
		private Margin CacheTabStripTabSizingMargin
		{
			get
			{
				if (UseDefaultSkin) return classCacheTabStripTabSizingMargin;
				else return objectCacheTabStripTabSizingMargin;
			}
		}

		#endregion

		/// <summary>
		/// Constructor of SkinnedRenderer
		/// </summary>
		public SkinnedRenderer()
			: base()
		{
		}

		#region Overrided Members

		/// <summary>
		/// Draw the auto hide bar background
		/// </summary>
		/// <param name="container">parent container</param>
		/// <param name="control">control</param>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		protected override void DrawAutoHideBarBackground(System.Windows.Forms.Control container, System.Windows.Forms.Control control, System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds)
		{
			if (Skin != null)
			{
				graphics.FillRectangle(new SolidBrush(Skin.CachedProperties.FormBackColor), bounds);
			}
			else
				base.DrawAutoHideBarBackground(container, control, graphics, bounds);
		}

		/// <summary>
		/// Draw the collapsed tab
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="dockSide">dock side</param>
		/// <param name="image">image</param>
		/// <param name="text">text</param>
		/// <param name="font">font</param>
		/// <param name="backColor">backcolor</param>
		/// <param name="foreColor">forecolor</param>
		/// <param name="state">state</param>
		/// <param name="vertical">is vertical?</param>
		protected override void DrawCollapsedTab(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, TD.SandDock.DockSide dockSide, System.Drawing.Image image, string text, System.Drawing.Font font, System.Drawing.Color backColor, System.Drawing.Color foreColor, System.Windows.Forms.DrawItemState state, bool vertical)
		{
			if (Skin != null)
				base.DrawCollapsedTab(graphics, bounds, dockSide, image, text, font, Skin.CachedProperties.FormBackColor, foreColor, state, vertical);
			else
				base.DrawCollapsedTab(graphics, bounds, dockSide, image, text, font, backColor, foreColor, state, vertical);
		}

		/// <summary>
		/// Draw the document strip's background
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		protected override void DrawDocumentStripBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds)
		{
			if (Skin != null && CacheDockWindowCaptionImgage != null)
				DrawImage(graphics, CacheDockWindowCaptionImgage, 2, 1, CacheDockWindowCaptionVerticalLayout, CacheDockWindowCaptionSizingMargin, bounds);
			else
				base.DrawDocumentStripBackground(graphics, bounds); 
		}

		/// <summary>
		/// Draw document strip button
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="buttonType">button type</param>
		/// <param name="state">state</param>
		protected override void DrawDocumentStripButton(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, TD.SandDock.Rendering.SandDockButtonType buttonType, System.Windows.Forms.DrawItemState state)
		{
			base.DrawDocumentStripButton(graphics, bounds, buttonType, state);
		}

		/// <summary>
		/// Draw document strip
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="contentBounds">content's bounds</param>
		/// <param name="image">image</param>
		/// <param name="text">text</param>
		/// <param name="font">font</param>
		/// <param name="backColor">backcolor</param>
		/// <param name="foreColor">forecolor</param>
		/// <param name="state">state</param>
		/// <param name="drawSeparator">draw separator or not?</param>
		protected override void DrawDocumentStripTab(Graphics graphics, Rectangle bounds, Rectangle contentBounds, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool drawSeparator)
		{
			if (Skin != null && CacheTabHeaderImgage != null)
			{
				int drawImage;

				switch (state)
				{
					default:
					case System.Windows.Forms.DrawItemState.Focus:
						drawImage = 2;
						break;
					case System.Windows.Forms.DrawItemState.Default:
						drawImage = 0;
						break;
					case System.Windows.Forms.DrawItemState.Grayed:
						drawImage = 3;
						break;
				}
				DrawImage(graphics, CacheTabHeaderImgage, 4, drawImage, CacheTabHeaderVerticalLayout, CacheTabHeaderSizingMargin, bounds);

				graphics.TextRenderingHint = Skin.TextRenderingHint;
				StringFormat sf = new StringFormat(StringFormat.GenericDefault);
				SizeF size = graphics.MeasureString(text, font, new SizeF(bounds.Width, bounds.Height), sf);
				graphics.DrawString(text, font, new SolidBrush(Skin.CachedProperties.FormForeColor), bounds.Left + (bounds.Width - size.Width) / 2, bounds.Top + (bounds.Height - size.Height) / 2);
			}
			else
				base.DrawDocumentStripTab(graphics, bounds, contentBounds, image, text, font, backColor, foreColor, state, drawSeparator);
		}

		/// <summary>
		/// Draw splitter
		/// </summary>
		/// <param name="container">container</param>
		/// <param name="control">control</param>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="orientation">orientation of splitter</param>
		protected override void DrawSplitter(System.Windows.Forms.Control container, System.Windows.Forms.Control control, System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Windows.Forms.Orientation orientation)
		{
			if (Skin != null)
			{
				graphics.FillRectangle(new SolidBrush(Skin.CachedProperties.FormBackColor), bounds);
			}
			else
				base.DrawSplitter(container, control, graphics, bounds, orientation);
		}

		/// <summary>
		/// Draw tab strip background
		/// </summary>
		/// <param name="container">container</param>
		/// <param name="control">control</param>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="selectedTabOffset">selected tab offset</param>
		protected override void DrawTabStripBackground(System.Windows.Forms.Control container, System.Windows.Forms.Control control, System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, int selectedTabOffset)
		{
			if (Skin != null)
				this.DrawDocumentStripBackground(graphics, bounds); // :)
			else
				base.DrawTabStripBackground(container, control, graphics, bounds, selectedTabOffset);
		}

		/// <summary>
		/// Draw tab strip tab
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="image">image</param>
		/// <param name="text">text</param>
		/// <param name="font">font</param>
		/// <param name="backColor">backcolor</param>
		/// <param name="foreColor">forecolor</param>
		/// <param name="state">state</param>
		/// <param name="drawSeparator">draw separator or not?</param>
		protected override void DrawTabStripTab(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Image image, string text, System.Drawing.Font font, System.Drawing.Color backColor, System.Drawing.Color foreColor, System.Windows.Forms.DrawItemState state, bool drawSeparator)
		{

			if (Skin != null && CacheTabStripTabImgage != null)
			{
				int drawImage;

				switch (state)
				{
					default:
					case System.Windows.Forms.DrawItemState.Selected:
						drawImage = 1;
						break;
					case System.Windows.Forms.DrawItemState.Default:
						drawImage = 3;
						break;
					case System.Windows.Forms.DrawItemState.Grayed:
						drawImage = 0;
						break;
				}
				DrawImage(graphics, CacheTabStripTabImgage, 4, drawImage, CacheTabStripTabVerticalLayout, CacheTabStripTabSizingMargin, bounds);

				graphics.DrawImage(image, new Rectangle(bounds.Location.X + 4, bounds.Location.Y + 4, 16, 16)); 

				StringFormat sf = new StringFormat(StringFormat.GenericDefault);
				SizeF size = graphics.MeasureString(text, font, new SizeF(bounds.Width, bounds.Height), sf);

				while (size.Width > bounds.Width - 21)
				{
					int limit = text.Length - 4;
					if (limit < 1) limit = 1;
					text = text.Substring(0, limit) + "..";
					size = graphics.MeasureString(text, font, new SizeF(bounds.Width - 5, bounds.Height - 2), sf);
				}
				graphics.TextRenderingHint = Skin.TextRenderingHint;
				graphics.DrawString(text, font, new SolidBrush(Skin.CachedProperties.FormForeColor), bounds.Left + 19 + (float)(bounds.Width - 19 - size.Width) / 2, bounds.Top + (float)(bounds.Height - size.Height) / 2);
			}
			else
				base.DrawTabStripTab(graphics, bounds, image, text, font, backColor, foreColor, state, drawSeparator);
		}

		/// <summary>
		/// Draw title bar background
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="focused">is focused?</param>
		protected override void DrawTitleBarBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, bool focused)
		{

			if (Skin != null && CacheDockWindowCaptionImgage != null)
			{

				int drawImage;

				if (focused)
					drawImage = 0;
				else
					drawImage = 1;
				DrawImage(graphics, CacheDockWindowCaptionImgage, 2, drawImage, CacheDockWindowCaptionVerticalLayout, CacheDockWindowCaptionSizingMargin, bounds);
			}
			else
				base.DrawTitleBarBackground(graphics, bounds, focused);

		}

		/// <summary>
		/// Draw title bar button
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="buttonType">button type</param>
		/// <param name="state">state</param>
		/// <param name="focused">is focused?</param>
		/// <param name="toggled">is toggled?</param>
		protected override void DrawTitleBarButton(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, TD.SandDock.Rendering.SandDockButtonType buttonType, System.Windows.Forms.DrawItemState state, bool focused, bool toggled)
		{
			if (Skin != null)
			{
				this.ActiveButtonBackgroundColor = Skin.CachedProperties.FormBackColor;
				this.InactiveButtonBackgroundColor = Skin.CachedProperties.FormBackColor;
			}

			base.DrawTitleBarButton(graphics, bounds, buttonType, state, focused, toggled);
		}
		
		/// <summary>
		/// Draw control client background
		/// </summary>
		/// <param name="graphics">graphics</param>
		/// <param name="bounds">bounds</param>
		/// <param name="backColor">backcolor</param>
		protected override void DrawControlClientBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Color backColor)
		{
			if (Skin != null)
				base.DrawControlClientBackground(graphics, bounds, Skin.CachedProperties.FormBackColor);
			else
				base.DrawControlClientBackground(graphics, bounds, backColor);

		}

		#endregion

		#region Deletable Members
		
		/*public override void DrawFakeTabControlBackgroundExtension(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Color backColor)
		{
			base.DrawFakeTabControlBackgroundExtension(graphics, bounds, cacheBackColor);
		}

		public override void DrawTabControlBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Color backColor, bool client)
		{
			base.DrawTabControlBackground(graphics, bounds, cacheBackColor, client);
		}

		/*public override void DrawTabControlButton(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, TD.SandDock.Rendering.SandDockButtonType buttonType, System.Windows.Forms.DrawItemState state)
		{
			//base.DrawTabControlButton(graphics, bounds, buttonType, state);
		}

		public override void DrawTabControlTab(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Image image, string text, System.Drawing.Font font, System.Drawing.Color backColor, System.Drawing.Color foreColor, System.Windows.Forms.DrawItemState state, bool drawSeparator)
		{
			//base.DrawTabControlTab(graphics, bounds, image, text, font, backColor, foreColor, state, drawSeparator);
		}

		public override void DrawTabControlTabStripBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Color backColor)
		{
			//base.DrawTabControlTabStripBackground(graphics, bounds, backColor);
		}

		public override void FinishRenderSession()
		{
			base.FinishRenderSession();
		}

		public override System.Drawing.Size MeasureTabControlTab(System.Drawing.Graphics graphics, System.Drawing.Image image, string text, System.Drawing.Font font, System.Windows.Forms.DrawItemState state)
		{
			return base.MeasureTabControlTab(graphics, image, text, font, state);
		}

		public override bool ShouldDrawControlBorder
		{
			get
			{
				return base.ShouldDrawControlBorder;
			}
		}

		public override bool ShouldDrawTabControlBackground
		{
			get
			{
				return base.ShouldDrawTabControlBackground;
			}
		}

		public override void StartRenderSession(System.Drawing.Text.HotkeyPrefix tabHotKeys)
		{
			base.StartRenderSession(tabHotKeys);
		}

		public override System.Drawing.Size TabControlPadding
		{
			get
			{
				return base.TabControlPadding;
			}
		}

		public override int TabControlTabExtra
		{
			get
			{
				return base.TabControlTabExtra;
			}
		}

		public override int TabControlTabHeight
		{
			get
			{
				return base.TabControlTabHeight;
			}
		}

		public override int TabControlTabStripHeight
		{
			get
			{
				return base.TabControlTabStripHeight;
			}
		}
		
		protected override void DrawDockContainerBackground(System.Drawing.Graphics graphics, TD.SandDock.DockContainer container, System.Drawing.Rectangle bounds)
		{
			base.DrawDockContainerBackground(graphics, container, bounds);
		}
		
		protected override void DrawDocumentClientBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, System.Drawing.Color backColor)
		{
			base.DrawDocumentClientBackground(graphics, bounds, cacheBackColor);
		}
		
		protected override void DrawTitleBarText(System.Drawing.Graphics graphics, System.Drawing.Rectangle bounds, bool focused, string text, System.Drawing.Font font)
		{
			base.DrawTitleBarText(graphics, bounds, focused, text, font);
		}

		/*protected override System.Drawing.Size MeasureDocumentStripTab(System.Drawing.Graphics graphics, System.Drawing.Image image, string text, System.Drawing.Font font, System.Windows.Forms.DrawItemState state)
		{
			return base.MeasureDocumentStripTab(graphics, image, text, font, state);
		}

		protected override System.Drawing.Size MeasureTabStripTab(System.Drawing.Graphics graphics, System.Drawing.Image image, string text, System.Drawing.Font font, System.Windows.Forms.DrawItemState state)
		{
			return base.MeasureTabStripTab(graphics, image, text, font, state);
		}

		protected override TD.SandDock.Rendering.BoxModel TabMetrics
		{
			get
			{
				return base.TabMetrics;
			}
		}

		protected override TD.SandDock.Rendering.BoxModel TabStripMetrics
		{
			get
			{
				return base.TabStripMetrics;
			}
		}

		protected override TD.SandDock.Rendering.TabTextDisplayMode TabTextDisplay
		{
			get
			{
				return base.TabTextDisplay;
			}
		}

		protected override TD.SandDock.Rendering.BoxModel TitleBarMetrics
		{
			get
			{
				return base.TitleBarMetrics;
			}
		}*/

		#endregion

		private void DrawImage(Graphics graphics, Image img, int imgCount, int drawImage, bool verticalLayout, Margin SizingMargin, System.Drawing.Rectangle bounds)
		{
			int imgWidth;
			int imgHeight;
			int imgTop;
			int imgLeft;

			if (!verticalLayout)
			{
				imgWidth = (Int32)(img.Width / imgCount);
				imgHeight = img.Height;
				imgTop = 0;
				imgLeft = drawImage * imgWidth;
			}
			else
			{
				imgWidth = img.Width;
				imgHeight = (Int32)(img.Height / imgCount);
				imgLeft = 0;
				imgTop = drawImage * imgHeight;
			}

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, SizingMargin.Left, SizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + SizingMargin.Left, imgTop, imgWidth - SizingMargin.Horizontal, SizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - SizingMargin.Right, imgTop, SizingMargin.Right, SizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + SizingMargin.Top, SizingMargin.Left, (imgHeight - 1) - SizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + SizingMargin.Left, imgTop + SizingMargin.Top, imgWidth - SizingMargin.Horizontal, (imgHeight - 1) - SizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - SizingMargin.Right, imgTop + SizingMargin.Top, SizingMargin.Right, (imgHeight - 1) - SizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - SizingMargin.Bottom, SizingMargin.Left, SizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + SizingMargin.Left, imgTop + imgHeight - SizingMargin.Bottom, imgWidth - SizingMargin.Horizontal, SizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - SizingMargin.Right, imgTop + imgHeight - SizingMargin.Bottom, SizingMargin.Right, SizingMargin.Bottom);
			#endregion

			#region Window parts
			Rectangle tLT = new Rectangle(bounds.Left, bounds.Top, SizingMargin.Left, SizingMargin.Top);
			Rectangle tT = new Rectangle(bounds.Left + SizingMargin.Left, bounds.Top, bounds.Width - SizingMargin.Horizontal, SizingMargin.Top);
			Rectangle tRT = new Rectangle(bounds.Right - SizingMargin.Right, bounds.Top, SizingMargin.Right, SizingMargin.Top);
			Rectangle tLM = new Rectangle(bounds.Left, bounds.Top + SizingMargin.Top, SizingMargin.Left, bounds.Height - SizingMargin.Vertical);
			Rectangle tM = new Rectangle(bounds.Left + SizingMargin.Left, bounds.Top + SizingMargin.Top, bounds.Width - SizingMargin.Horizontal, bounds.Height - SizingMargin.Vertical);
			Rectangle tRM = new Rectangle(bounds.Right - SizingMargin.Right, bounds.Top + SizingMargin.Top, SizingMargin.Right, bounds.Height - SizingMargin.Vertical);
			Rectangle tLB = new Rectangle(bounds.Left, bounds.Bottom - SizingMargin.Bottom, SizingMargin.Left, SizingMargin.Bottom);
			Rectangle tB = new Rectangle(bounds.Left + SizingMargin.Left, bounds.Bottom - SizingMargin.Bottom, bounds.Width - SizingMargin.Horizontal, SizingMargin.Bottom);
			Rectangle tRB = new Rectangle(bounds.Right - SizingMargin.Right, bounds.Bottom - SizingMargin.Bottom, SizingMargin.Right, SizingMargin.Bottom);
			#endregion

			#region Drawing
			graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);

			#endregion

		}

		private static void InicializeClassCache(object sender, EventArgs e)
		{
			Skin skin = sender as Skin;

			if (skin != null)
			{
				XmlNode cNode = skin.SelectSingleNode("Elements/Form/Color");

				classCacheDockWindowCaptionImgage = skin.GetImageFromBitmapResource(skin.KeyPrefix + "docking.dockwindowcaption.png");
				classCacheDockWindowCaptionSizingMargin = new Margin(skin.SelectSingleNode("Elements/DockWindowCaption/Image/SizingMargins"));
				classCacheDockWindowCaptionVerticalLayout = false;
				if (skin.SelectSingleNode("Elements/DockWindowCaption/Image").Attributes["Layout"] != null)
				{
					if (skin.SelectSingleNode("Elements/DockWindowCaption/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						classCacheDockWindowCaptionVerticalLayout = true;
					}
				}

				classCacheTabHeaderImgage = skin.GetImageFromBitmapResource(skin.KeyPrefix + "docking.tabheader.png");
				classCacheTabHeaderSizingMargin = new Margin(skin.SelectSingleNode("Elements/TabHeader/Image/SizingMargins"));
				classCacheTabHeaderVerticalLayout = false;
				if (skin.SelectSingleNode("Elements/TabHeader/Image").Attributes["Layout"] != null)
				{
					if (skin.SelectSingleNode("Elements/TabHeader/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						classCacheTabHeaderVerticalLayout = true;
					}
				}

				classCacheTabStripTabImgage = (classCacheTabHeaderImgage.Clone() as Image);
				classCacheTabStripTabImgage.RotateFlip(RotateFlipType.RotateNoneFlipY);
				Padding p = new Padding(classCacheTabHeaderSizingMargin.Left, classCacheTabHeaderSizingMargin.Bottom, classCacheTabHeaderSizingMargin.Right, classCacheTabHeaderSizingMargin.Top);
				classCacheTabStripTabSizingMargin = new Margin(p);
				classCacheTabStripTabVerticalLayout = classCacheTabHeaderVerticalLayout;


			}

		}

		private void InicializeObjectCache(object sender)
		{
			Skin skin = sender as Skin;

			if (skin != null)
			{
				XmlNode cNode = skin.SelectSingleNode("Elements/Form/Color");

				objectCacheDockWindowCaptionImgage = skin.GetImageFromBitmapResource(skin.KeyPrefix + "docking.dockwindowcaption.png");
				objectCacheDockWindowCaptionSizingMargin = new Margin(skin.SelectSingleNode("Elements/DockWindowCaption/Image/SizingMargins"));
				objectCacheDockWindowCaptionVerticalLayout = false;
				if (skin.SelectSingleNode("Elements/DockWindowCaption/Image").Attributes["Layout"] != null)
				{
					if (skin.SelectSingleNode("Elements/DockWindowCaption/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						objectCacheDockWindowCaptionVerticalLayout = true;
					}
				}

				objectCacheTabHeaderImgage = skin.GetImageFromBitmapResource(skin.KeyPrefix + "docking.tabheader.png");
				objectCacheTabHeaderSizingMargin = new Margin(skin.SelectSingleNode("Elements/TabHeader/Image/SizingMargins"));
				objectCacheTabHeaderVerticalLayout = false;
				if (skin.SelectSingleNode("Elements/TabHeader/Image").Attributes["Layout"] != null)
				{
					if (skin.SelectSingleNode("Elements/TabHeader/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						objectCacheTabHeaderVerticalLayout = true;
					}
				}

				objectCacheTabStripTabImgage = (objectCacheTabHeaderImgage.Clone() as Image);
				objectCacheTabStripTabImgage.RotateFlip(RotateFlipType.RotateNoneFlipY);
				Padding p = new Padding(objectCacheTabHeaderSizingMargin.Left, objectCacheTabHeaderSizingMargin.Bottom, objectCacheTabHeaderSizingMargin.Right, objectCacheTabHeaderSizingMargin.Top);
				objectCacheTabStripTabSizingMargin = new Margin(p);
				objectCacheTabStripTabVerticalLayout = objectCacheTabHeaderVerticalLayout;


			}

		}

		#region ISkinnable Members 

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				if (value && !mUseDefaultSkinsEventInUse)
				{
					DefaultLookAndFeel.OnSkinChanged += new EventHandler(InicializeClassCache);
					mUseDefaultSkinsEventInUse = true;

					InicializeClassCache(DefaultLookAndFeel.Skin, new EventArgs());
				}
				if (!value)
				{
					DefaultLookAndFeel.OnSkinChanged -= new EventHandler(InicializeClassCache);
					mUseDefaultSkinsEventInUse = false;
				}

				mUseDefaultSkin = value;
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				InicializeObjectCache(mSkin);
			}
		}

		#endregion
	}
}
